<?php

namespace app\admin\controller;

use think\Image;

class Upload extends Backend
{
    /**
     * @var array
     * 不需要权限认证的控制器/方法（*表示该控制器的所有方法都不需要权限认证）
     */
    protected $noNeedAuth = ['*'];

    /**
     * 上传图片
     * Author: LCY<409028735@qq.com>
     * Time: 2021/12/4 15:59
     * Note:
     */
    public function image()
    {
        if ($this->request->isPost()) {
            $upload_dir = '/uploads/image'; //上传目录的位置
            $width = $this->request->param('_width/d', 500);
            $height = $this->request->param('_height/d', 500);
            //裁剪模式，0：不裁剪，
            //1：标识缩略图等比例缩放类型，
            //2：标识缩略图缩放后填充类型
            //3：标识缩略图居中裁剪类型
            //4：标识缩略图左上角裁剪类型
            //5：标识缩略图右下角裁剪类型
            //6：标识缩略图固定尺寸缩放类型
            $crop = $this->request->param('_crop/d', 0);
            $file = $this->request->file('file');
            $upload_path = env('root_path') . 'public' . $upload_dir; //上传目录的绝对路径

            if ($file) {
                try {
                    $info = $file->move($upload_path);
                } catch (\Exception $e) {
                    //适应前端上传上传组件dropzone
                    header('HTTP/1.1 500 server error');
                }
                if ($info) {
                    //裁剪生成缩略图
                    if ($crop >= 1 && $crop <= 6) {
                        $path = $upload_path . '/' . $info->getSaveName(); //保存到服务器的绝对路径
                        $image = Image::open($path);
                        $image->thumb($width, $height, $crop)->save($path);
                    }

                    $url = $upload_dir . '/' . str_replace('\\', '/', $info->getSaveName());
                    $this->success('ok', '', ['url' => $url]);
                } else {
                    header('HTTP/1.1 400' . $file->getError());
                }
            } else {
                header('HTTP/1.1 400 No File');
            }
        } else {
            $type = $this->request->param('_type/s', 'one');
            $url = $this->request->param('_url/s', '');
            $num = $this->request->param('_num/d', 1);
            $width = $this->request->param('_width/d', 500);
            $height = $this->request->param('_height/d', 500);
            $crop = $this->request->param('_crop/d', 0);
            $size = $this->request->param('_size/f', 1);
            $accept = $this->request->param('_accept/s', '');
            $field = $this->request->param('_field/s', '');

            $this->assign('upload_config', [
                'type' => $type,
                'url' => $url . '?_crop=' . $crop . '&_width=' . $width . '&_height=' . $height,
                'num' => intval($num),
                'size' => floatval($size),
                'accept' => $accept,
                'field' => $field,
                'width' => $width,
                'height' => $height,
                'crop' => $crop
            ]);
            return $this->fetch();
        }
    }

    /**
     * 针对tinymce组件的文件上传的处理
     * @return mixed
     * Author: LCY<409028735@qq.com>
     * Time: 2021/12/23 15:32
     * Note:
     */
    public function tinymce()
    {
        if ($this->request->isPost()) {
            $upload_dir = '/uploads/image'; //上传目录的位置
            $file = $this->request->file('file');
            $upload_path = env('root_path') . 'public' . $upload_dir; //上传目录的绝对路径

            if ($file) {
                try {
                    $info = $file->move($upload_path);
                } catch (\Exception $e) {
                    return ajax_return(0, $e->getMessage());
                }
                if ($info) {
                    $url = $upload_dir . '/' . str_replace('\\', '/', $info->getSaveName());
                    return ajax_return(1, 'ok', '', ['url' => $url]);
                } else {
                    return ajax_return(0, '上传有误');
                }
            } else {
                return ajax_return(0, '上传有误');
            }
        }
    }

    public function file()
    {
        if ($this->request->isPost()) {
            $upload_dir = '/uploads/file'; //上传目录的位置
            $file = $this->request->file('file');
            $upload_path = env('root_path') . 'public' . $upload_dir; //上传目录的绝对路径

            if ($file) {
                try {
                    $info = $file->move($upload_path);
                } catch (\Exception $e) {
                    header('HTTP/1.1 500 server error');
                }
                if ($info) {
                    $url = $upload_dir . '/' . str_replace('\\', '/', $info->getSaveName());
                    $this->success('ok', '', ['url' => $url]);
                } else {
                    header('HTTP/1.1 400' . $file->getError());
                }
            } else {
                header('HTTP/1.1 400 No File');
            }
        } else {
            $type = $this->request->param('_type/s', 'one');
            $url = $this->request->param('_url/s', '');
            $num = $this->request->param('_num/d', 1);
            $size = $this->request->param('_size/f', 1);
            $accept = $this->request->param('_accept/s', '');
            $field = $this->request->param('_field/s', '');

            $this->assign('upload_config', [
                'type' => $type,
                'url' => $url,
                'num' => intval($num),
                'size' => floatval($size),
                'accept' => $accept,
                'field' => $field,
            ]);
            return $this->fetch();
        }
    }
}
